package mcommand;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.geom.Rectangle2D;

import javax.swing.ImageIcon;

/**
 * This class handles which image icon should be painted, destroyed or alive it
 * also handles the paint method for city and some position methods.
 *
 * @author Marcel, Christian, Klaus
 */
public class City {
    // defines where to find the images for the alive and destroyed city
    public static String PATH_IMAGE_CITYALIVE = "assets/city.png";
    public static String PATH_IMAGE_CITYDESTROYED = "assets/citydestroyed.png";
    
    // initiates the image variables
    public static Image cityAlive;
    public static Image cityDestroyed;
    
    // initiate the town state variable
    private TownState ts;
    
    // initiates the position variables
    private int pos_x;
    private int pos_y;

    /**
     * defines the city alive and destroyed image, if they are not set already
     * (checks that) sets the TownState to ALIVE.
     */
    public City() {
        // if the cityAlive variable has no value
        if (cityAlive == null) {
            // tries to load the image
            try {
                ImageIcon ii = new ImageIcon(this.getClass().getResource(
                        PATH_IMAGE_CITYALIVE));
                cityAlive = ii.getImage();
                // catches the Exception if something went wrong and prints a
                // message on the console out
            } catch (Exception e) {
                System.err.println("City Alive konnte nicht geladen werden!");
            }
        }
        // if the cityDestroyed variable has no value
        if (cityDestroyed == null) {
            // tries to load the image
            try {
                ImageIcon ii = new ImageIcon(this.getClass().getResource(
                        PATH_IMAGE_CITYDESTROYED));
                cityDestroyed = ii.getImage();
                // catches the Exception if something went wrong and prints a
                // message on the console out
            } catch (Exception e) {
                System.err
                        .println("City Destroyed konnte nicht geladen werden!");
            }
        }
        // sets the TownState to ALIVE
        ts = TownState.ALIVE;
    }

    /**
     * Sets the position for the city.
     * 
     * @param x x
     * @param y y
     */
    public final void setPosition(final int x, final int y) {
        pos_x = x;
        pos_y = y;
    }

    /**
     * Returns the x Position of the city.
     * 
     * @return x
     */
    public final int getX() {
        return pos_x;
    }

    /**
     * Returns the y Position of the city.
     * 
     * @return y
     */
    public final int getY() {
        return pos_y;
    }

    /**
     * Sets the TownState.
     * 
     * @param ts ts
     */
    public final void setState(final TownState ts) {
        this.ts = ts;
    }

    /**
     * Draws the right image for the current TownState.
     * 
     * @param g2d g2d
     */
    public final void paint(final Graphics2D g2d) {
        if (ts == TownState.ALIVE) {
            g2d.drawImage(cityAlive, pos_x, pos_y, null);
        } else if (ts == TownState.DESTROYED) {
            g2d.drawImage(cityDestroyed, pos_x, pos_y, null);
        }
    }

    /**
     * Returns true if the city is ALIVE, either false.
     * 
     * @return true or false
     */
    public final boolean isAlive() {
        if (ts == TownState.ALIVE) {
            return true;
        }

        return false;
    }

    /**
     * Returns the center point of the new point of (20,20).
     * 
     * @return a point
     */
    public final Point getCenterPoint() {
        return new Point(20, 20);
    }

    /**
     * Returns a new rectangle in size of the city image for collision handling.
     * 
     * @return new rectangle
     */
    public final Rectangle2D getCityRectangle() {
        return new Rectangle2D.Float(pos_x, pos_y, cityAlive.getWidth(null),
                cityAlive.getHeight(null));
    }

    /**
     * Holds the 2 TownStates (ALIVE, DESTROYED).
     * 
     * 
     */
    public enum TownState {
        ALIVE,
        DESTROYED
    }
}
